<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/laypage.css">
<script type="text/javascript" src="js/jquery/jquery.min.js"></script>
<script type="text/javascript" src="js/laypage/laypage.js"></script>
<script type="text/javascript" src="js/layer/layer.js"></script>
<script type="text/javascript" src="js/vue/vue.min.js"></script>
<body>
<div id="app" class="container">

    <form class="form-inline bg-danger" role="form">
        查询条件：
        <div class="form-group">
            <label class="sr-only" for="dname">部门名称</label>
            <input type="text" class="form-control" id="dname" placeholder="部门名称">
        </div>
        <div class="form-group">
            <label class="sr-only" for="loc">部门地址</label>
            <input type="text" class="form-control" id="loc" placeholder="部门地址">
        </div>
        <button type="button" id="findDept" class="btn btn-success">查询部门</button>
        <button type="button" id="addDeptBtn" class="btn btn-danger">增加部门</button>
    </form>

    <div class="table-responsive">
        <table class="table">
            <thead>
            <tr class="success">
                <td>序号</td>
                <td>部门名称</td>
                <td>部门地址</td>
                <td>操作</td>
            </tr>
            </thead>

            <tbody>
            <tr class="active" v-for="(item,index) in result">
                <td>{{index+1}}</td>
                <td>{{item.dname}}</td>
                <td>{{item.loc}}</td>
                <td><a href="#" @click = "editEvent(item.deptid)">编辑</a>|<a href="#" @click="delEvent(item.deptid)">删除</a></td>
            </tr>
            </tbody>
        </table>
        <div id="pagenav"></div>
    </div>
</div>
<%--保存当前页码--%>
<input type="hidden" id="curr">
</body>
</html>
<script>
    var app = new Vue({
        el: '#app',
        data: {
            result: []
        }
    });
    //查询
    var getDeptPageList = function (curr) {
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: '/json/dept',
            data: {
                pageNum: curr || 1,
                pageSize:3,
                dname: $("#dname").val(),
                loc : $("#loc").val()
            },
            success: function (msg) {
                app.result = msg.result;
                $("#curr").val(msg.pageNum);
                laypage({
                    cont: 'pagenav',
                    pages: msg.totalPage,
                    skip: true,
                    skin: '#7B68EE',
                    curr: curr || 1,
                    jump: function (obj, first) {
                        if (!first) {
                            getDeptPageList(obj.curr);
                        }
                    }
                });
            }

        });
    }

    getDeptPageList();
    $("#findDept").on("click",function () {
        getDeptPageList();
    })
    //新增
    $("#addDeptBtn").on("click",function () {
        layer.open({
            type:2,
            title:'新增部门',
            fix:false,
            maxmin:true,
            shadeClose:true,
            area:['800px','500px'],
            content:'addDept.html',
            end: function(){
                //更新页面信息
                getDeptPageList($("#curr").val());
            }
        })
    })
    //删除
    var delEvent = function (deptid) {
        layer.confirm('确定要删除吗？',{
            btn : ['是','否']
        },function () {
            //是
            $.ajax({
                type:'GET',
                dataType:'json',
                url:'/json/delDept',
                data:{
                    deptid:deptid
            },success:function (msg) {
                    getDeptPageList();
                    layer.msg('删除成功'+deptid,{icon:6});
                }
            });
            //否
        },function () {
        });
    }
    //编辑
    var editEvent = function (deptid) {
        layer.open({
            type: 2,
            title: '修改部门',
            fix: false,
            maxmin: true,
            shadeClose: true,
            area: ['800px', '500px'],
            content: '/json/findDept?deptid='+deptid,
            end: function () {
                //更新页面信息
                getDeptPageList($("#curr").val());
            }
        });
    }

</script>
